package com.mingqijia.gassafety.shared.constant;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mingqijia.gassafety.db.entity.redis.DetectorMeasure;
import com.mingqijia.gassafety.db.entity.redis.DtuMeasure;
import com.mingqijia.gassafety.db.entity.redis.LatestDetector;

import java.util.List;

public class EquipmentStatus {
    public static final int offline = 0;
    public static final int normal = 1;
    public static final int lowAlert = 2;
    public static final int highAlert = 3;
    public static final int broken = 4;
    //屏蔽
    public static final int sheild = 5;
    //紫荆WiFi报警器报警
    public static final int alert = 6;

    /**
     * 设备状态详情
     * @param iMEI
     * @param equipmentType
     * @param latestDetector
     * @return status
     */
    public static JSONObject getMsg(String iMEI, String equipmentType , LatestDetector latestDetector){
        JSONObject json = JSONUtil.createObj();
        EquipmentType type = EquipmentType.parse(equipmentType);
        if (type == null) {
            return null;
        }
        DtuMeasure dtuMeasure = latestDetector.getDtuMeasure();
        //点型可燃气体探测器
        if (type.getCode().equals(EquipmentType.POINT_TYPE_GAS_DETECTOR.getCode())) {
            List<DetectorMeasure> detectorMeasures = latestDetector.getDetectorMeasures();
            if (detectorMeasures !=null && detectorMeasures.size()>0){
                for (DetectorMeasure detectorMeasure : detectorMeasures) {
                    if (iMEI.equals(latestDetector.getDtuId()+"-"+detectorMeasure.getSubId())) {
                        switch  (detectorMeasure.getStatus()){
                            //正常
                            case normal:
                                json.set("status",normal);
                                json.set("msg","");
                                return json;
                            case offline:
                                json.set("status",offline);
                                json.set("msg","");
                                return json;
                            case lowAlert:
                                json.set("status",lowAlert);
                                json.set("msg","发生报警，低报");
                                return json;
                            case highAlert:
                                json.set("status",highAlert);
                                json.set("msg","发生报警，高报");
                                return json;
                            case broken:
                                json.set("status",broken);
                                json.set("msg","");
                                if (dtuMeasure != null) {
                                    if (dtuMeasure.getMpb()) {
                                        json.set("msg","报警控制器主电故障");
                                    }
                                    if (dtuMeasure.getBpb()) {
                                        json.set("msg","报警控制器备电故障");
                                    }
                                    if (dtuMeasure.getCb()) {
                                        json.set("msg","报警控制器充电故障");
                                    }
                                }
                                return json;
                            case sheild:
                                json.set("status",sheild);
                                json.set("msg","");
                                return json;
                            default:
                                return null;
                        }
                    }
                }
            }
        } else if(type.getCode().equals(EquipmentType.INDEPENDENT_GAS_DETECTOR.getCode()) ||
                type.getCode().equals(EquipmentType.HOME_GAS_DETECTOR.getCode())){
            //独立式可燃气体探测器  家用报警器
            List<DetectorMeasure> detectorMeasures = latestDetector.getDetectorMeasures();
            if (detectorMeasures !=null && detectorMeasures.size()>0) {
                for (DetectorMeasure detectorMeasure : detectorMeasures) {
                    switch  (detectorMeasure.getStatus()){
                        //正常
                        case normal:
                            json.set("status",normal);
                            json.set("msg","");
                            if (dtuMeasure != null) {
                                if (dtuMeasure.getCg()) {
                                    json.set("msg","正常，正在充电");
                                } else {
                                    json.set("msg","正常，未充电");
                                }
                            }
                            return json;
                        case offline:
                            json.set("status",offline);
                            json.set("msg","");
                            return json;
                        case lowAlert:
                            json.set("status",lowAlert);
                            json.set("msg","发生报警，低报");
                            return json;
                        case highAlert:
                            json.set("status",highAlert);
                            json.set("msg","发生报警，高报");
                            return json;
                        case broken:
                            json.set("status",broken);
                            json.set("msg","");
                            if (dtuMeasure != null) {
                                if (dtuMeasure.getMpb()) {
                                    json.set("msg","主电故障");
                                }
                                if (dtuMeasure.getBpb()) {
                                    json.set("msg","备电故障");
                                }
                                if (dtuMeasure.getCb()) {
                                    json.set("msg","充电故障");
                                }
                            }
                            return json;
                        case sheild:
                            json.set("status",sheild);
                            json.set("msg","");
                            return json;
                        default:
                            return null;
                    }
                }
            }
        } else if (type.getCode().equals(EquipmentType.GAS_ALARM_CONTROLLER.getCode())){
            //报警控制器
            if (dtuMeasure != null) {
                if (!dtuMeasure.getAl() && !dtuMeasure.getBk() && !dtuMeasure.getComErr()) {
                    json.set("status",normal);
                    if (dtuMeasure.getCg()) {
                        json.set("msg","正常，正在充电");
                    } else {
                        json.set("msg","正常，未充电");
                    }
                } else if (dtuMeasure.getBk()){
                    //报警信息探头已显示详情，故这里只需显示报警状态即可
                    json.set("status",broken);
                    json.set("msg","");
                    if (dtuMeasure.getMpb()) {
                        json.set("msg","主电故障");
                    }
                    if (dtuMeasure.getBpb()) {
                        json.set("msg","备电故障");
                    }
                    if (dtuMeasure.getCb()) {
                        json.set("msg","充电故障");
                    }
                } else if (dtuMeasure.getComErr()) {
                    //离线
                    json.set("status",offline);
                    json.set("msg","");
                } else if (dtuMeasure.getAl()){
                    //报警信息探头已显示详情，故这里只需显示报警状态即可
                    json.set("status",normal);
                    json.set("msg","");
                }
                return json;
            }
        }
        return null;
    }

}
